package com.zerosky.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zerosky.crm.po.Group;
import com.zerosky.crm.po.Machine;
import com.zerosky.crm.po.Revenue;
import com.zerosky.crm.po.Score;
import com.zerosky.crm.service.GroupService;
import com.zerosky.crm.service.MachineService;
import com.zerosky.crm.service.RevenueService;
import com.zerosky.crm.service.ScoreService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

/**
 * 系统设置（角色、员工） 相关业务
 * 
 * @author
 *
 */
@Controller
public class GroupController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(GroupController.class); // 日志
	
	@Resource(name = "groupService")
	private GroupService groupService;
	
	@Resource(name = "machineService")
	private MachineService machineService;
	
	@Resource(name = "scoreService")
	private ScoreService scoreService;
	
	@Resource(name = "revenueService")
	private RevenueService revenueService;
	
	

	/**
	 * 去分组 jsp页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toGroupJsp.do")
	public String toRoleJsp(HttpServletRequest request, HttpSession session) {
			return "WEB-INF/jsp/group/groupList";

	}
	
	/**
	 * 去单机 jsp页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toMachineJsp.do")
	public String toMachineJsp(HttpServletRequest request, HttpSession session) {
		List<Group> list =groupService.getGroupAllList(null);
		session.setAttribute("groupList", list);
		return "WEB-INF/jsp/group/machineList";

	}
	
	/**
	 * 去收益 jsp页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toRevenueJsp.do")
	public String toRevenueJsp(HttpServletRequest request, HttpSession session) {
		List<Group> list =groupService.getGroupAllList(null);
		session.setAttribute("groupList", list);
			return "WEB-INF/jsp/group/revenueList";

	}
	
	/**
	 * 去收益 jsp页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toScoreJsp.do")
	public String toScoreJsp(HttpServletRequest request, HttpSession session) {
		List<Group> list =groupService.getGroupAllList(null);
		session.setAttribute("groupList", list);
			return "WEB-INF/jsp/group/scoreList";

	}

	/**
	 * 分组查询 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("queryGroup.do")
	@ResponseBody
	public Map<String, Object> queryGroup(HttpSession session, HttpServletRequest request, Integer limit, Integer page,String groupName) {
		Map<String, Object> json = new HashMap<String, Object>();
			Map<String, Object> param = new HashMap<String, Object>();
			try {
				// if((branchName == "" || branchName == null) && company !=
				// null && company != ""){
				// param.put("queryByCompany", 1);
				// }
				param.put("groupName", groupName);
				param.put("limit", limit);
				param.put("page", page);
				PageInfo<Group> pageInfo = groupService.getGroupPageInfoList(param);
				json.put("total", pageInfo.getPageCount());
				// json.put("darw", darw+1);
				json.put("data", pageInfo.getList());
				json.put("code", "1");

			} catch (Exception e) {
				json.put("code", "-1");
				logger.error("获取数据失败，原因：" + e);
			}
		return json;
	}
	
	/**
	 * AJAX 异步 添加分组信息
	 * 
	 * @param request
	 * @param response
	 * @return 1成功 -1异常 0失败
	 */
	@RequestMapping("addGroup.do")
	@ResponseBody
	public Map<String, Object> addGroup(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			Group group) {
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
			try {
				group.setCreateDate(new Date());
				groupService.addGroup(group);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	
	/**
	 * 删除分组信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping("delGroup.do")
	@ResponseBody
	public Map<String, Object> delGroup(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			String id) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				groupService.deleteGroupById(id);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	
	/**
	 * 根据id查询出分组详细信息
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @return
	 */
	@RequestMapping("groupDetail.do")
	@ResponseBody
	public Map<String, Object> groupDetail(HttpSession session, HttpServletRequest request,
			HttpServletResponse response, String id) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				Group group = groupService.getGroupById(id);
				json.put("group", group);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;

	}
	
	/**
	 * 编辑信息
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @return
	 */
	@RequestMapping("editGroup.do")
	@ResponseBody
	public Map<String, Object> editGroup(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			Group group) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				groupService.updateGroup(group);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	/**
	 * 获取全部组信息
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @return
	 */
	@RequestMapping("getAllGroup.do")
	@ResponseBody
	public Map<String, Object> getAllGroup(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				List<Group> list =groupService.getGroupAllList(json);
				json.put("list", list);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	
	//***************************************************************************************************
	/**
	 * 机查询 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("queryMachine.do")
	@ResponseBody
	public Map<String, Object> queryMachine(HttpSession session, HttpServletRequest request, Integer limit, Integer page,String machineName,Integer groupId) {
		Map<String, Object> json = new HashMap<String, Object>();
			Map<String, Object> param = new HashMap<String, Object>();
			try {
				// if((branchName == "" || branchName == null) && company !=
				// null && company != ""){
				// param.put("queryByCompany", 1);
				// }
				param.put("machineName", machineName);
				param.put("groupId", groupId);
				param.put("limit", limit);
				param.put("page", page);
				PageInfo<Machine> pageInfo = machineService.getMachinePageInfoList(param);
				json.put("total", pageInfo.getPageCount());
				// json.put("darw", darw+1);
				json.put("data", pageInfo.getList());
				json.put("code", "1");

			} catch (Exception e) {
				json.put("code", "-1");
				logger.error("获取数据失败，原因：" + e);
			}
		return json;
	}
	
	/**
	 * AJAX 异步 添加机信息
	 * 
	 * @param request
	 * @param response
	 * @return 1成功 -1异常 0失败
	 */
	@RequestMapping("addMachine.do")
	@ResponseBody
	public Map<String, Object> addMachine(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			Machine machine) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				machine.setCreateDate(new Date());
				machineService.addMachine(machine);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	
	/**
	 * 删除机信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping("delMachine.do")
	@ResponseBody
	public Map<String, Object> delMachine(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			String id) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				machineService.deleteMachineById(id);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	
	/**
	 * 根据id查询出机详细信息
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @return
	 */
	@RequestMapping("machineDetail.do")
	@ResponseBody
	public Map<String, Object> machineDetail(HttpSession session, HttpServletRequest request,
			HttpServletResponse response, String id) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				Machine machine = machineService.getMachineById(id);
				json.put("machine", machine);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;

	}
	
	/**
	 * 编辑机信息
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @return
	 */
	@RequestMapping("editMachine.do")
	@ResponseBody
	public Map<String, Object> editMachine(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			Machine machine) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				machineService.updateMachine(machine);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	/**
	 * 获取全部机信息
	 * 
	 * @param request
	 * @param response
	 * @param role
	 * @return
	 */
	@RequestMapping("getAllMachine.do")
	@ResponseBody
	public Map<String, Object> getAllMachine(HttpSession session, HttpServletRequest request, HttpServletResponse response,String groupId) {
		Map<String, Object> json = new HashMap<String, Object>();
			try {
				json.put("groupId", groupId);
				List<Machine> list =machineService.getMachineAllList(json);
				json.put("list", list);
				json.put("code", 1);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				json.put("code", -1);
			}
		return json;
	}
	
	//***************************************************************************************************
		/**
		 * 分数查询 分页查询
		 * 
		 * @param request
		 * @return
		 */
		@RequestMapping("queryScore.do")
		@ResponseBody
		public Map<String, Object> queryScore(HttpSession session, HttpServletRequest request, Integer limit, Integer page,Integer machineId) {
			Map<String, Object> json = new HashMap<String, Object>();
				Map<String, Object> param = new HashMap<String, Object>();
				try {
					// if((branchName == "" || branchName == null) && company !=
					// null && company != ""){
					// param.put("queryByCompany", 1);
					// }
					param.put("machineId", machineId);
					param.put("limit", limit);
					param.put("page", page);
					PageInfo<Score> pageInfo = scoreService.getScorePageInfoList(param);
					json.put("total", pageInfo.getPageCount());
					// json.put("darw", darw+1);
					json.put("data", pageInfo.getList());
					json.put("code", "1");

				} catch (Exception e) {
					json.put("code", "-1");
					logger.error("获取数据失败，原因：" + e);
				}
			return json;
		}
		
		/**
		 * AJAX 异步 添加分数信息
		 * 
		 * @param request
		 * @param response
		 * @return 1成功 -1异常 0失败
		 */
		@RequestMapping("addScore.do")
		@ResponseBody
		public Map<String, Object> addScore(HttpSession session, HttpServletRequest request, HttpServletResponse response,
				Score score) {
			Map<String, Object> json = new HashMap<String, Object>();
				try {
					score.setCreateDate(new Date());
					if(scoreService.addScore(score)>0){
						Score sc=scoreService.getScoreById(score.getMachineId().toString());
						if(sc!=null){
							Float money = (float) ((score.getScore()-sc.getScore())/10);
							Revenue revenue = new Revenue();
							revenue.setCreateDate(new Date());
							revenue.setMachineId(score.getMachineId());
							revenue.setMoney((double)money);
							revenueService.addRevenue(revenue);
							System.out.println(money);
						}else{
							Float money = (float) ((score.getScore()-0)/10);
							Revenue revenue = new Revenue();
							revenue.setCreateDate(new Date());
							revenue.setMachineId(score.getMachineId());
							revenue.setMoney((double)money);
							revenueService.addRevenue(revenue);
							System.out.println(money);
						}
						json.put("code", 1);
					}else{
						json.put("code", -1);
					}
				} catch (Exception e) {
					logger.error("获取数据失败，原因：" + e);
					json.put("code", -1);
				}
			return json;
		}
		//***************************************************************************************************
				/**
				 * 收益查询 分页查询
				 * 
				 * @param request
				 * @return
				 */
				@RequestMapping("queryRevenue.do")
				@ResponseBody
				public Map<String, Object> queryRevenue(HttpSession session, HttpServletRequest request, Integer limit, Integer page,Integer machineId,String startDate,String endDate) {
					Map<String, Object> json = new HashMap<String, Object>();
						Map<String, Object> param = new HashMap<String, Object>();
						try {
							// if((branchName == "" || branchName == null) && company !=
							// null && company != ""){
							// param.put("queryByCompany", 1);
							// }
							param.put("startDate", startDate);
							param.put("endDate", endDate);
							param.put("machineId", machineId);
							param.put("limit", 999999999);
							param.put("page", page);
							PageInfo<Revenue> pageInfo = revenueService.getRevenuePageInfoList(param);
							json.put("total", pageInfo.getPageCount());
							// json.put("darw", darw+1);
							json.put("data", pageInfo.getList());
							json.put("code", "1");

						} catch (Exception e) {
							json.put("code", "-1");
							logger.error("获取数据失败，原因：" + e);
						}
					return json;
				}
}
